﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessModel;
using EntityDataModel;
using System.ComponentModel;
using Controller.DataProvider;
using System.Data;
using BaseUtilities;

namespace Controller
{
    /// <summary>
    /// * Unnormal Properties : SaleOrderDetail, CbbUnitChange
    /// </summary>
    public class SaleProductCtr : INotifyPropertyChanged
    {
        #region Properties
        // SaleOrderPro.
        private SaleOrderPro _SaleOrderPro;
        public SaleOrderPro SaleOrderPro
        {
            get
            {
                if (_SaleOrderPro == null)
                {
                    _SaleOrderPro = new SaleOrderPro();
                }
                return _SaleOrderPro;
            }
            set { _SaleOrderPro = value; }
        }
        // SaleOrderDetailPro.
        private SaleOrderDetailPro _SaleOrderDetailPro;
        public SaleOrderDetailPro SaleOrderDetailPro
        {
            get
            {
                if (_SaleOrderDetailPro == null)
                {
                    _SaleOrderDetailPro = new SaleOrderDetailPro();
                }
                return _SaleOrderDetailPro;
            }
            set { _SaleOrderDetailPro = value; }
        }
        // UnitChangePro.
        private UnitChangePro _UnitChangePro;
        public UnitChangePro UnitChangePro
        {
            get
            {
                if (_UnitChangePro == null)
                {
                    _UnitChangePro = new UnitChangePro();
                }
                return _UnitChangePro;
            }
            set { _UnitChangePro = value; }
        }



        // SaleOrder.
        private SaleOrder _SaleOrder;
        public SaleOrder SaleOrder
        {
            get
            {
                if (_SaleOrder == null)
                {
                    _SaleOrder = new SaleOrder();
                }
                return _SaleOrder;
            }
            set { _SaleOrder = value; OnPropertyChanged("SaleOrder"); }
        }
        // SaleOrderDetail.
        private SaleOrderDetail _SaleOrderDetail;
        public SaleOrderDetail SaleOrderDetail
        {
            get
            {
                if (_SaleOrderDetail == null)
                {
                    _SaleOrderDetail = new SaleOrderDetail();
                }
                return _SaleOrderDetail;
            }
            set { _SaleOrderDetail = value; OnPropertyChanged("SaleOrderDetail"); }
        }

        // CbbCodeProduct. List of IdNameModel for Product.
        private IList<IdNameModel> _CbbCodeProduct;
        public IList<IdNameModel> CbbCodeProduct
        {
            get
            {
                if (_CbbCodeProduct == null)
                {
                    _CbbCodeProduct = ProductPro.GetAllCode().ToResult<IList<IdNameModel>>();
                }
                return _CbbCodeProduct;
            }
            set { _CbbCodeProduct = value; }
        }
        // CbbNameProduct. List of IdNameModel for Product.
        private IList<IdNameModel> _CbbNameProduct;
        public IList<IdNameModel> CbbNameProduct
        {
            get
            {
                if (_CbbNameProduct == null)
                {
                    _CbbNameProduct = ProductPro.GetAllName().ToResult<IList<IdNameModel>>();
                }
                return _CbbNameProduct;
            }
            set { _CbbNameProduct = value; }
        }

        // CbbProduct. List of IdNameModel for Product.
        private IList<Product> _CbbProduct;
        public IList<Product> CbbProduct
        {
            get
            {
                if (_CbbProduct == null)
                {
                    _CbbProduct = ProductPro.GetAllCbb().ToResult<IList<Product>>();
                    if (_CbbProduct != null)
                        _CbbProduct = _CbbProduct.Where(p => p.Quantity.HasValue && p.Quantity.Value > 0).ToList();
                }
                return _CbbProduct;
            }
            set { _CbbProduct = value; }
        }

        // CbbUnitChange. List of IdNameModel for UnitChange.
        private IList<UnitChange> _CbbUnitChange;
        public IList<UnitChange> CbbUnitChange
        {
            get
            {
                //if (_CbbUnitChange == null)
                //{
                    //_CbbUnitChange = UnitChangePro.GetsByProductId(ProductId, DateTime.Now.Date).ToResult<IList<UnitChange>>();
                //}
                return _CbbUnitChange;
            }
            set { _CbbUnitChange = value; OnPropertyChanged("CbbUnitChange"); }
        }

        #region Binding to View
        //ProductId
        private int _ProductId;
        public int ProductId
        {
            get { return _ProductId; }
            set
            {
                if (_ProductId != value)
                {
                    _ProductId = value;
                    IList<UnitChange> lstUnitChanges = UnitChangePro.GetsByProductId(value, DateTime.Now.Date).ToResult<IList<UnitChange>>();
                    CbbUnitChange = new List<UnitChange>();
                    foreach (UnitChange item in lstUnitChanges)
                    {
                        UnitChange oUnitChange = CbbUnitChange.Where(p => p.UnitId == item.UnitId).FirstOrDefault();
                        if (oUnitChange == null)
                        {
                            CbbUnitChange.Add(item);
                        }
                    }
                    OnPropertyChanged("ProductId");
                }
            }
        }
         //ProductCode
        private string _ProductCode;
        public string ProductCode
        {
            get { return _ProductCode; }
            set
            {
                if (_ProductCode != value)
                {
                    _ProductCode = value;
                    OnPropertyChanged("ProductCode");
                }
            }
       }
        //ProductName
        private string _ProductName;
        public string ProductName
        {
            get { return _ProductName; }
            set
            {
                if (_ProductName != value)
                {
                    _ProductName = value;
                    OnPropertyChanged("ProductName");
                }
            }
        }
        //ProductUnitId
        private int _ProductUnitId;
        public int ProductUnitId
        {
            get { return _ProductUnitId; }
                  set
            {
                if (_ProductUnitId != value)
                {
                    _ProductUnitId = value;
                    OnPropertyChanged("ProductUnitId");
                }
            }
  }
        //ProductQuantity
        private int _ProductQuantity;
        public int ProductQuantity
        {
            get { return _ProductQuantity; }
            set
            {
                if (_ProductQuantity != value)
                {
                    _ProductQuantity = value;
                    OnPropertyChanged("ProductQuantity");
                }
            }
        }
        //ProductSalePriceUnit
        private int _ProductSalePriceUnit;
        public int ProductSalePriceUnit
        {
            get { return _ProductSalePriceUnit; }
            set
            {
                if (_ProductSalePriceUnit != value)
                {
                    _ProductSalePriceUnit = value;
                    OnPropertyChanged("ProductSalePriceUnit");
                }
            }
        }
        //ProductTotal
        private int _ProductTotal;
        public int ProductTotal
        {
            get { return _ProductTotal; }
            set
            {
                if (_ProductTotal != value)
                {
                    _ProductTotal = value;
                    OnPropertyChanged("ProductTotal");
                }
            }
        }
        #endregion Binding to View




        // ProductPro.
        private ProductPro _ProductPro;
        public ProductPro ProductPro
        {
            get
            {
                if (_ProductPro == null)
                {
                    _ProductPro = new ProductPro();
                }
                return _ProductPro;
            }
            set { _ProductPro = value; }
        }
        // Product.
        private Product _Product;
        public Product Product
        {
            get
            {
                if (_Product == null)
                {
                    _Product = new Product();
                }
                return _Product;
            }
            set { _Product = value; OnPropertyChanged("Product"); }
        }
        // LstProduct.
        private IList<Product> _LstProduct;
        public IList<Product> LstProduct
        {
            get 
            {
                if (_LstProduct == null)
                {
                    _LstProduct = GetAll().ToResult<IList<Product>>();
                }
                return _LstProduct;
            }
            set { _LstProduct = value; }
        }
        // SelectedRows.
        private IList<SaleOrderDetail> _SelectedRows;
        public IList<SaleOrderDetail> SelectedRows
        {
            get
            {
                if (_SelectedRows == null)
                {
                    _SelectedRows = new List<SaleOrderDetail>();
                }
                return _SelectedRows;
            }
            set { _SelectedRows = value; }
        }
        #endregion Properties

        #region Public Methods
        //----------------------------------------
        public ResultModel Save(Product pProduct)
        {
            EntityKey oEntityKey = new EntityKey(BaseProvider.QualifyName + "." + ProductPro.EntityName, "Id", pProduct.Id);
            return ProductPro.Save(pProduct, oEntityKey);
        }
        //----------------------------------------
        public ResultModel DeleteById(int pId)
        {
            EntityKey oEntityKey = new EntityKey(BaseProvider.QualifyName + "." + ProductPro.EntityName, "Id", pId);
            return ProductPro.DeleteById(oEntityKey);
        }
        //----------------------------------------
        public ResultModel GetById(int pId)
        {
            EntityKey oEntityKey = new EntityKey(BaseProvider.QualifyName + "." + ProductPro.EntityName, "Id", pId);
            return ProductPro.GetById(oEntityKey);
        }
        //----------------------------------------
        public ResultModel GetAll()
        {
            return ProductPro.GetAll();
        }
        //----------------------------------------
        public ResultModel GetAllByTypeId(int pTypeId)
        {
            return ProductPro.GetAllByTypeId(pTypeId);
        }
        // Extend Methods.
        //----------------------------------------
        public ResultModel GetTypeByProductId(int pProductId)
        {
            return ProductPro.GetTypeById(pProductId);
        }
        public ResultModel GetCodeAll()
        {
            return ProductPro.GetIdNameAll("Id", "Code");
        }
        //----------------------------------------
        public ResultModel GetIdNameAll()
        {
            return ProductPro.GetIdNameAll();
        }
        //----------------------------------------
        public ResultModel GetIdNameAllByTypeId(int pTypeId)
        {
            return ProductPro.GetIdNameAllByTypeId(pTypeId);
        }
        //----------------------------------------
        public ResultModel GetUnitAll()
        {
            return (new UnitPro()).GetIdNameAll();
        }
        //----------------------------------------
        public ResultModel GetSupplierAll()
        {
            return (new SupplierPro()).GetIdNameAll();
        }
        //----------------------------------------
        public ResultModel GetDiscountTypeAll()
        {
            return (new DiscountTypePro()).GetIdNameAll();
        }
        //----------------------------------------
        public ResultModel GetUnitChangeByProductId(int pProductId)
        {
            return this.UnitChangePro.GetsByProductId(pProductId);
        }
        //----------------------------------------
        public ResultModel GetUnitChangeByProductId(int pProductId, DateTime pApplyDate)
        {
            return this.UnitChangePro.GetsByProductId(pProductId, pApplyDate);
        }
        #endregion Public Methods
        //----------------------------------------
        public ResultModel SaveSaleOrder(SaleOrder pSaleOrder)
        {
            EntityKey oEntityKey = new EntityKey(BaseProvider.QualifyName + "." + ProductPro.EntityName, "Id", pSaleOrder.Id);
            return SaleOrderPro.SaveSaleOrderFull(pSaleOrder);
        }

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string pPropertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(pPropertyName));
        }
        #endregion
    }
}
